﻿using ArangoDB.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestArangoDB2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// add static method to append log to richtext box.
        /// </summary>
        public async void AppendText(string str)
        {

            await Task.Run(() =>
            {
                Console.WriteLine("");
                this.Invoke((Action)delegate
                {
                    this.richTextBox1.AppendText(str + "\r\n");
                });

            });

        }
        private void button1_Click(object sender, EventArgs e)
        {
            ArangoDatabase.ChangeSetting(s =>
            {
                s.Database = "testdb1";
                s.Url = "http://localhost:8529";

                // you can set other settings if you need
                s.Credential = new NetworkCredential("tiger", "hzh1660");
                s.SystemDatabaseCredential = new NetworkCredential("root", "hzh1660");
            });

            using (var db = ArangoDatabase.CreateWithSetting())
            {
                db.CreateCollection("person");
                db.CreateCollection("Friend", type: CollectionType.Edge);
                var person = new Person
                {
                    Fullname = "raoof hojat",
                    Age = 28
                };

               // db.Insert<Person>(person);
                db.Collection("person").Insert(new Person
                {
                    Fullname = "raoof hojat",
                    Age = 28
                });
                person.Age = 35;

                // partially updates person document, only `Age` attribute will be updated
                db.Update<Person>(person);
                db.UpdateById<Person>("document-key", new { Age = 40 });
                var loadedPeson = db.Document<Person>("document-key");
                var result = db.Query<Person>().Where(p => p.Age == 35).Select(p => p.Fullname).ToList();

                AppendText("end");
            }
        }
    }
    public class Person
    {
        [DocumentProperty(Identifier = IdentifierType.Key)]
        public string Key { get; set; }

        public string Fullname { get; set; }

        public int Age { get; set; }
    }
}
